﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;

namespace NHINDirect
{
    public class OutgoingMessage : NHINDMessage
    {
        NHINDAddressCollection m_recipients;
        NHINDAddressCollection m_rejectedRecipients;

        public OutgoingMessage()
        {
        }

        public override Header To
        {
            get
            {
                return base.To;
            }
            set
            {
                base.To = value;
                this.m_recipients = null;
            }
        }
        
        public NHINDAddressCollection Recipients
        {
            get
            {
                if (this.m_recipients == null)
                {
                    this.m_recipients = this.ToAddressCollection<NHINDAddress, NHINDAddressCollection>(this.To, x => new NHINDAddress(x));                    
                }
                
                return this.m_recipients;
            }
        }
        
        public bool HasRecipients
        {
            get
            {
                NHINDAddressCollection recipients = this.Recipients;
                return (recipients != null && recipients.Count > 0);
            }            
        }
        
        public NHINDAddressCollection RejectedRecipients
        {
            get
            {
                if (this.m_rejectedRecipients == null)
                {
                    this.m_rejectedRecipients = new NHINDAddressCollection();
                }
                
                return this.m_rejectedRecipients;
            }
        }
        
        public bool HasRejectedRecipients
        {
            get
            {
                return (this.m_rejectedRecipients != null && this.m_rejectedRecipients.Count > 0);
            }
        }

        internal void CategorizeRecipients(TrustEnforcementStatus minTrustStatus)
        {
            this.m_rejectedRecipients = NHINDAddressCollection.Create(this.Recipients.GetUntrusted(minTrustStatus));
            this.Recipients.RemoveUntrusted(minTrustStatus);            
        }
        
        internal void UpdateTo()
        {
            this.ParsedTo = this.Recipients.ToMailAddressCollection();
        }
    }
}
